package redisson.rest.topic;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ThreadLocalRandom;

import org.redisson.api.RTopic;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@RestController
@RequestMapping("topic")
public class TopicProducer {

	@Autowired
	RedissonClient redissonClient;

	@GetMapping("send")
	public String send() {

		ThreadLocalRandom random = ThreadLocalRandom.current();
		String str = "test:topic:%s";
		RTopic topic = redissonClient.getTopic(String.format(str, "axxc"));

		int nextInt = random.nextInt(5, 10);
		for (int i = 0; i < nextInt; i++) {

			Map<Object, Object> map = new HashMap<>();
			map.put("AA", "AA");
			map.put("BB", "BB");
			map.put("CC", "CC");

			long publish = topic.publish(map);

			log.info("### 发送redisson消息成功,,, {} ", publish);

		}

		return new String("发送消息" + nextInt + "成功!");
	}

}
